package com.wealth.scoremarket.web.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.wealth.cart.entity.CartGoodsInfo;
import com.wealth.cart.entity.CartItem;
import com.wealth.cart.service.ICartItemService;
import com.wealth.cart.service.ICartService;
import com.wealth.commodity.entity.Goods;
import com.wealth.commodity.service.IGoodsService;
import com.wealth.member.entity.Address;
import com.wealth.member.entity.Membership;
import com.wealth.member.service.IAddressService;
import com.wealth.member.service.IMemberService;
import com.wealth.member.service.IMembershipService;
import com.wealth.order.service.IOrderPlatService;
import com.wealth.platform.entity.MerchantRate;
import com.wealth.platform.service.IMerchantRateService;
import com.wealth.system.web.controller.BaseController;
/**
 * 购物车
 * @author zjw
 *
 */
@Controller
@RequestMapping(value = "/manage/score/cart")
public class CartController  extends BaseController {

	Logger logger = Logger.getLogger(CartController.class);
	
	@Autowired
	private ICartService cartService;
	
	@Autowired
	private ICartItemService icartItemService;
	
	@Autowired
	private IGoodsService igoodsService;
	
	@Autowired
	private IMemberService imemberService;
	
	@Autowired
	private IMerchantRateService merchantRateService;
  
	@Autowired
	IMembershipService iMembershipService;
	
	@Autowired
	IAddressService iAddressService;
	
	@Autowired
	IOrderPlatService iOrderPlatService;
	
	
	@RequestMapping(value = "/index")
	public String toCart(Model model){
		try { 
			 
			//积分一元转换率
			MerchantRate merchantRate = merchantRateService.findMerchantRateByCorpId(corpid);
			//购物车
			List<CartGoodsInfo> cartGoodsList  = cartService.getCartGoodsByMemberId(memberid,4l);
			double allPrice = 0;
			double allCartPrice = 0;//购物车里所有的商品价格
			for (int i = 0; i < cartGoodsList.size(); i++) {
				allPrice+= cartGoodsList.get(i).getTotalPrice();
				
				for (int j = 0; j < cartGoodsList.get(i).getList().size(); j++) {
					allCartPrice += (cartGoodsList.get(i).getList().get(j).getPrice()*cartGoodsList.get(i).getList().get(j).getAmount());
				}
			}
			model.addAttribute("cartGoodsList", cartGoodsList);
			model.addAttribute("allCartPrice", allCartPrice);
			model.addAttribute("allPrice", allPrice);
			model.addAttribute("merchantRate", merchantRate);
			return "scoremarket/cart/index";
		} catch (Exception e) {
			logger.error("购物车首页错误！！！", e);
			return "system/500";
		}
		
	}
	
	
	 /**
	  * 加入购物车
	  * @param model
	  * @param userId 会员id
	  * @param goodsId 商品id
	  * @return
	  */
	@RequestMapping(value = "/addCart/{goodsId}")
	public String addCart(Model model,@PathVariable long goodsId ,int amount){
		try {
			 
			 Goods goods = igoodsService.get(goodsId); 
			 cartService.addCart(memberid,amount, goods,4l);
			
			return "redirect:/manage/score/cart/index";
		} catch (Exception e) {
			logger.error("加入购物车错误！！！", e);
			return "system/500";
		}
		
	}
	
	
	
	
	@RequestMapping(value = "/ordersubmit")
	public String toOrder(Model model){
		try { 
			//积分一元转换率
			MerchantRate merchantRate = merchantRateService.findMerchantRateByCorpId(corpid);
			List<CartItem> cartItemList = icartItemService.findCartItemByCartId(memberid, 4L, 2);
			
			double price = 0;
			
			for (int i = 0; i < cartItemList.size(); i++) {
				price+= cartItemList.get(i).getAmount()*cartItemList.get(i).getGoods().getPrice();
			}
			//会员信息 
			Membership membership = iMembershipService.get(memberid);
			
			//默认地址
			Address address = iAddressService.getAddressDefault(memberid);
			model.addAttribute("address", address);
			model.addAttribute("membership", membership);
			model.addAttribute("cartItemList", cartItemList);
			model.addAttribute("merchantRate", merchantRate);
			model.addAttribute("totalscore", price*merchantRate.getScore());
			return "scoremarket/cart/ordersubmit";
		} catch (Exception e) {
			logger.error("提交订单页面首页错误！！！", e);
			return "system/500";
		}
		
	}
	
	@RequestMapping(value = "/submitOrder",method = RequestMethod.POST) 
	public String submitOrder(HttpServletRequest request,Model model,Long addressid){
		
		try { 
			 //商户id  会员id  市场id  地址di  优惠券  支付方式
		    iOrderPlatService.saveOrder(corpid, memberid, 4l, addressid, -1l, 2,1L);
		    return "redirect:/manage/order/status/3";
		} catch (Exception e) {
			logger.error("积分商城提交订单错误！！！", e);
			e.printStackTrace();
		}
		return null;
	}
}
